/*
 * @Title: 中奖信息确认
 * @Author: JSL
 * @Date:   2018-12-21
 * @Last Modified by:   JSL
 * @Last Modified time: 2018-12-22
 */

var dataInfo = new Object(),
  template = {
    init: function() {
      dataInfo.init = new Vue({
        el: '#app',
        data: {
          prizeRecord: JSON.parse(unescape(getCookie('zmtMallOrder'))),
          show: false,
          type: false, //判断用户是否有默认地址
          isWx: is_weixn(),
          addressid: !getQueryString('addressId') ? '' : getQueryString('addressId'), //地址列表返回id
          buyWay: 4, //1-立即购买 2-购物车 3-拼团 4-中奖信息确认
          list: [], //商品信息列表
          address: [], //回显的地址信息
          addAdress: { //提交的地址信息
            userId: userId,
            name: '',
            phone: '',
            province: '',
            city: '',
            district: '',
            address: '',
            defaultAddress: 0
          },
          submitSwitch: 1,
          areaList: addressInfo, //选择地址的数据集合
        },
        methods: {
          choseOver: function(data) { //用户选择地址结束
            this.addAdress.province = data[0].name;
            this.addAdress.city = ' ' + data[1].name + ' ';
            this.addAdress.district = data[2].name;
            this.show = false;
          },
          getDefaulAddressByUser: function() { //获取默认地址
            ajaxTemplate(addressTest + SERVICE.getDefaulAddressByUser, function(data) {
              if (data.tag == 0) {
                if (data.data.result.length == 0) {
                  dataInfo.init.type = false;
                } else {
                  dataInfo.init.type = true;
                  dataInfo.init.addressid = data.data.result[0].id;
                  dataInfo.init.address = data.data.result;
                  document.body.scrollTop = 0;
                };
                dataInfo.init.$toast.clear();
              } else {
                dataInfo.init.$toast.clear();
                dataInfo.init.$toast(data.errMsg);
              };
              dataInfo.init.initProductInfo();
            }, {
              userId: userId
            });
          },
          getAddressDetails: function() { //获取默认地址 - 外部跳转来的
            ajaxTemplate(addressTest + SERVICE.getOrderDetails, function(data) {
              if (data.tag == 0) {
                dataInfo.init.type = true;
                dataInfo.init.addressid = data.data.result[0].id;
                dataInfo.init.address = data.data.result;
                dataInfo.init.$toast.clear();
              } else {
                dataInfo.init.$toast.clear();
                dataInfo.init.$toast(data.errMsg);
              };
              dataInfo.init.initProductInfo();
            }, {
              userId: userId,
              id: this.addressid
            });
          },
          saveAddress: function() { //新增默认地址
            if (!this.addAdress.name || !this.addAdress.name.trim()) {
              this.$toast('请输入收货人姓名');
            } else if (!this.addAdress.phone) {
              this.$toast('请输入收货人手机号');
            } else if (!(/^1[0-9]\d{9}$/.test(this.addAdress.phone))) {
              this.$toast('请输入正确的手机号');
            } else if (!this.addAdress.district) {
              this.$toast('请选择收货区域');
            } else if (!this.addAdress.address || !this.addAdress.address.trim()) {
              this.$toast('请输入详细地址');
            } else {
              this.$toast.loading({
                forbidClick: true,
                message: '收货信息提交中...',
                loadingType: 'spinner',
                duration: 0,
              });
              ajaxTemplate(addressTest + SERVICE.addAddressOrder, function(data) {
                console.log(data);
                if (data.tag == 0) {
                  dataInfo.init.getDefaulAddressByUser();
                } else {
                  dataInfo.init.$toast.clear();
                  dataInfo.init.$toast(data.errMsg);
                };
              }, JSON.parse(JSON.stringify(this.addAdress)));
            };
          },
          submitH5Order: function() { //提交订单
            if (!this.type) {
              this.$toast('请填写收货地址');
            } else {
              dataInfo.init.$toast.loading({
                forbidClick: true,
                message: '数据提交中...',
                loadingType: 'spinner',
                duration: 0,
              });
              if (this.submitSwitch == 1) {
                this.submitSwitch = 2;
                ajaxTemplate(addressUrl + SERVICE.orderPrizeSubmit, function(data) {
                  console.log(data);
                  if (data.tag == 0) {
                    location.href = '../html/FPtPrizeInit.html?initStatus=2';
                  } else {
                    dataInfo.init.$toast.clear();
                    dataInfo.init.$dialog.alert({
                      message: data.errMsg
                    }).then();
                  };
                  dataInfo.init.submitSwitch = 1;
                }, {
                  userId: userId,
                  recordId: this.prizeRecord.id,
                  addressId: this.addressid
                });
              };
            };
          },
          toAddressList: function() { //跳转地址列表
            location.href = '../html/addressList.html?from=orderPrize.html&buyWay=' + this.buyWay;
          },
          initProductInfo: function() { //获取当前中奖信息的商品详情
            ajaxTemplate(addressUrl + SERVICE.queryGoodsByGoodsCode, function(data) {
              if (data.tag == 0) {
                dataInfo.init.list = data.data.result;
              } else {
                dataInfo.init.$toast.clear();
                dataInfo.init.$toast(data.errMsg);
              };
            }, {
              goodsCode: this.prizeRecord.remarks
            });
          },
        },
      });

      // 初始化数据
      if (!dataInfo.init.addressid) { //没有地址id获取默认地址
        dataInfo.init.getDefaulAddressByUser();
      } else if (!!dataInfo.init.addressid) { //有地址id 获取地址id
        dataInfo.init.getAddressDetails();
      };

    },
  };

template.init();
